<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>申请证照</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="100px" ref="editForm">
          <el-form-item label="证照名称:" prop="name">
            <el-select v-model="editForm.licence_id" placeholder="请选择证照">
              <el-option
                  v-for="item in arryList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="借用用途:" prop="name">
            <el-input v-model="editForm.purpose" auto-complete="off" placeholder="填写借用用途"></el-input>
          </el-form-item>
          <el-form-item label="使用日期:" prop="name">
            <el-date-picker type="date"
                            placeholder="选择日期"
                            v-model="editForm.time"
                            :picker-options="pickerOptions1"
                            value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <el-form-item label="归还日期:" prop="name">
            <el-date-picker type="date"
                            placeholder="选择日期"
                            v-model="editForm.return_time"
                            :picker-options="pickerOptions1"
                            value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <el-form-item label="补充说明:" prop="name">
            <el-input v-model="editForm.remark" auto-complete="off" placeholder="补充说明" type="textarea"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">返回</el-button>
        <el-button size="small" type="primary" @click.native="editSubmit">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  props: ['visible', 'licenceId'], // commoent 学生点评 collection 学生采集
  components: {},
  data() {
    return {
      dialogVisible: false,
      arryList: [],
      editForm: {
        licence_id: '', // 证照id
        purpose: '', // 用途
        time: '', // 预计使用时间
        return_time: '', // 预计归还时间
        remark: ''
      }, // 编辑界面数据
      // 限制时间选择
      pickerOptions1:
          {
            disabledDate(time) {
              return time.getTime() < Date.now() - 8.64e7
            }
          },
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.licenceId) {
          this.getDetail();
        }
        this.getLicenceList()
      } else {
        for (let i in this.editForm) {
          this.editForm[i] = '';
        }
      }
    }
  },
  created() {
  },
  methods: {
    getLicenceList() {
      this.$fetch(this.api.licence.index, {p: 1}).then((response) => {
        if (response.code === 0) {
          this.arryList = this.getJson(response.list.data)
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    getDetail() {
      this.$fetch(this.api.licence.edit, {id: this.licenceId}).then((response) => {
        if (!response.code) {
          this.editForm.name = response.vo.name;
          this.editForm.user_id = response.vo.user ? response.vo.user.id : '';
          this.examine_user = this.getJson(response.vo.user)
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    // 提交数据
    async editSubmit() {
      // 验证信息
      let dataOption = this.getJson(this.editForm)
      if (!dataOption.licence_id) {
        this.$message.error('选择需要申请的证照')
        return
      }
      if (!dataOption.time) {
        this.$message.error('选择使用日期')
        return
      }
      if (!dataOption.return_time) {
        this.$message.error('选择归还日期')
        return
      }

      let url = this.api.licence.apply;

      // if (this.licenceId) {
      //   url = this.api.licence.edit
      //   dataOption.id = this.licenceId
      // }


      let data = await this.$post(url, dataOption);
      if (!data.code) {
        this.$message.success('编辑成功！')
        this.$emit('InitFn')
        this.closePOpup()
      } else {
        this.$message.error(data.msg)
      }
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 50%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.hearder-search {
  display: flex;
  align-items: center;
}
</style>


